package infostroy.bookagolf.components.dialogs;

import com.sun.awt.AWTUtilities;
import infostroy.bookagolf.common.Constants.Icons;
import infostroy.bookagolf.common.helpers.ImageHelper;
import infostroy.bookagolf.components.panels.BaseDraggablePanel;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

/**
 * Created by valer_000 on 2/9/14.
 */
public abstract class GolfBaseDialog  {

    protected Color titleColor;
    protected Color background;
    protected JDialog dialog;
    protected JLabel titleLbl;
    protected JPanel contentPanel;
    protected Dimension dimension;

    /**
     * Create dialog without system decoration.
     * @param title dialog title
     * @param background dialog background color
     */
    public GolfBaseDialog(String title, Color background, Color titleColor) {
        this.background = background;
        this.titleColor = titleColor;
        this.titleLbl = new JLabel(title);
        this.contentPanel = new JPanel(new BorderLayout());
    }

    public GolfBaseDialog(String title, Color background, Dimension dimension, Color titleColor) {
        this(title, background, titleColor);
        this.dimension = dimension;
    }

    protected void prepareDialog() {
        this.dialog = new JDialog();
        this.dialog.setModal(true);
        if (dimension != null) {
            this.dialog.setSize(dimension.width, dimension.height);
        } else {
            this.dialog.setSize(250, 30);
        }

        this.dialog.setUndecorated(true);
        this.dialog.setLocationRelativeTo(null);
        this.dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.dialog.setModal(true);
        this.contentPanel.setBackground(this.background);
        this.contentPanel.setBorder(new LineBorder(Color.BLACK, 1));
        this.dialog.add(this.contentPanel, BorderLayout.CENTER);
        AWTUtilities.setWindowOpaque(dialog, false);
    }

    private void initDialog() {
        this.prepareDialog();
        this.initDialogHeader();
        this.initContent();
        this.dialog.setVisible(true);

    }

    protected abstract void initContent();

    protected void initDialogHeader() {
        BaseDraggablePanel panel = new BaseDraggablePanel(this.dialog);
        panel.setLayout(new BorderLayout());
        panel.setBackground(new Color(1, 1, 1, 0));
        panel.setBorder(new EmptyBorder(5, 15, 0 ,0));

        titleLbl.setForeground(titleColor);
        panel.add(titleLbl, BorderLayout.LINE_START);
        panel.add(initCloseButton(Icons.RED_MARK), BorderLayout.LINE_END);

        this.contentPanel.add(panel, BorderLayout.NORTH);
    }

    protected JButton initCloseButton(String imgUrl) {
        JButton button = new JButton();
        BufferedImage icon = ImageHelper.getInstance().loadImage(imgUrl);
        if (icon != null) {
            button.setIcon(new ImageIcon(icon));
            button.setBorderPainted(false);
            button.setFocusPainted(false);
            button.setContentAreaFilled(false);
            button.setMaximumSize(new Dimension(icon.getWidth(), icon.getHeight()));
            button.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    dialog.setVisible(false);
                }
            });
        }
        return button;
    }

    public void setTitle(String title) {
        this.titleLbl.setText(title);
    }

    public void showDialog() {
        if (this.dialog == null) {
            initDialog();
        } else {
            this.initContent();
            this.dialog.setLocationRelativeTo(null);
            this.dialog.setVisible(true);
        }
    }

    public boolean isVisible(){
        if(this.dialog != null){
            return this.dialog.isVisible();
        }
        else{
            return false;
        }

    }
    public void setVisible(boolean visible){
        this.dialog.setVisible(visible);
    }
}
